package com.example.fourtestapp.ui;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.fourtestapp.R;
import com.example.fourtestapp.entity.UserInfo;
import com.example.fourtestapp.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.LogRecord;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity{


    private TextView RegNameTxt;
    private EditText RegNameET;
    private TextView RegPwdTxt;
    private EditText RegPwdET;
    private Spinner RegSexSP;
    private Button RegOkBT;
    private Button RegReturnBT;
    private UserInfo userInfo=new UserInfo();
    private List<String> Sexs = new ArrayList<>();



    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 200:
                    Toast.makeText(RegistActivity.this, ""+msg.obj.toString(),  Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(RegistActivity.this,LoginActivity.class));
                    finish();
                    break;
                case 500:
                    Toast.makeText(RegistActivity.this, ""+msg.obj.toString(),  Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initListeren();
    }



    private void initView() {
        RegNameTxt = findViewById(R.id.RegNameTxt);
        RegNameET = findViewById(R.id.RegNameET);
        RegPwdTxt = findViewById(R.id.RegPwdTxt);
        RegPwdET = findViewById(R.id.RegPwdET);
        RegSexSP = findViewById(R.id.RegSexSP);
        RegOkBT = findViewById(R.id.RegOkBT);
        RegReturnBT = findViewById(R.id.RegReturnBT);
        Sexs.add("男");
        Sexs.add("女");
        Sexs.add("保密");
        ArrayAdapter adapter = new ArrayAdapter(RegistActivity.this, android.R.layout.simple_list_item_1, Sexs);
        RegSexSP.setAdapter(adapter);
    }

    private  void  initListeren(){
        RegOkBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpUtils.Get("http://liushenlong.cn/api/user?name="+RegNameET.getText()+"&pwd="+RegPwdET.getText()+"&sex="+RegSexSP.getSelectedItem().toString()
                        , new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Toast.makeText(RegistActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String data = response.body().string();
                                userInfo = new Gson().fromJson(data, UserInfo.class);
                                Message message = new Message();
                                message.what=userInfo.getCode();
                                message.obj=userInfo.getMsg();
                                handler.sendMessage(message);
                            }
                        });
            }
        });


        RegReturnBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RegistActivity.this,LoginActivity.class));
                finish();
            }
        });
    }




    }

